from flask import Flask, jsonify, render_template
import json
from map import load_gps_data, wgs84_to_gcj02

app = Flask(__name__)

# 读取gps数据并进行纠偏
def load_and_correct_gps_data(filename):
    gps_data = load_gps_data(filename)
    # 对每个坐标点进行纠偏
    corrected_data = []
    for data in gps_data:
        corrected_coords = wgs84_to_gcj02(data["LBSLON"], data["LBSLAT"])
        corrected_data.append({
            "LBSLON": corrected_coords[0],
            "LBSLAT": corrected_coords[1]
        })
    return corrected_data

# API端点，返回纠偏后的 GPS 数据
@app.route('/api/gps_data')
def get_corrected_gps_data():
    gps_data = load_and_correct_gps_data("gps_data.txt")  # 读取并纠偏数据
    return jsonify(gps_data)

# 主页面，渲染 HTML 页面
@app.route('/')
def index():
    return render_template('map.html')  # 假设你将HTML文件命名为 index.html

if __name__ == '__main__':
    app.run(debug=True)
